<?php
declare(strict_types=1);

namespace app\job;

use app\service\IpLocationService;
use think\facade\{Db, Log};
use think\queue\Job;

class IpLocationJob
{
    /**
     * 执行IP地理位置查询任务
     */
    public function fire(Job $job, $data)
    {
        try {
            $ipService = new IpLocationService();
            $location = $ipService->getLocation($data['ip']);
            
            if ($location) {
                // 根据不同类型更新对应表的地理位置信息
                switch ($data['type']) {
                    case 'admin':
                        $this->updateAdminLoginLog($data['log_id'], $location);
                        break;
                    case 'merchant':
                        $this->updateMerchantLoginLog($data['log_id'], $location);
                        break;
                    case 'user':
                        $this->updateUserLoginLog($data['log_id'], $location);
                        break;
                }
                $job->delete();
            } else {
                // 查询失败,延迟重试
                $attempts = $job->attempts();
                if ($attempts > 3) {
                    $job->delete();
                    return;
                }
                
                // 递增延迟:1分钟、5分钟、15分钟
                $delay = match($attempts) {
                    1 => 60,
                    2 => 300,
                    3 => 900
                };
                
                $job->release($delay);
            }
        } catch (\Exception $e) {
            Log::error('IP地理位置更新失败: ' . $e->getMessage());
            $job->delete();
        }
    }
    
    /**
     * 更新管理员登录日志
     */
    protected function updateAdminLoginLog($logId, $location)
    {
        Db::name('admin_login_log')->where('id', $logId)->update([
            'country' => $location['country'],
            'province' => $location['province'],
            'city' => $location['city'],
            'district' => $location['district'],
            'isp' => $location['isp'],
            'update_time' => date('Y-m-d H:i:s')
        ]);
    }
    
    /**
     * 更新商家登录日志
     */
    protected function updateMerchantLoginLog($logId, $location)
    {
        Db::name('merchant_login_log')->where('id', $logId)->update([
            'country' => $location['country'],
            'province' => $location['province'],
            'city' => $location['city'],
            'district' => $location['district'],
            'isp' => $location['isp'],
            'update_time' => date('Y-m-d H:i:s')
        ]);
    }
    
    /**
     * 更新用户登录日志
     */
    protected function updateUserLoginLog($logId, $location)
    {
        Db::name('user_login_log')->where('id', $logId)->update([
            'country' => $location['country'],
            'province' => $location['province'],
            'city' => $location['city'],
            'district' => $location['district'],
            'isp' => $location['isp'],
            'update_time' => date('Y-m-d H:i:s')
        ]);
    }
} 